import { TableInfo } from "@/entities/tableInfo.entity";
import createFile from "./createFile";


/**
 * 创建路由代码
 * @param name 
 * @param data 
 */
const createRouterCode = async (data: TableInfo) => {
    const { name, fieldInfoList = [], indexInfoList = [] } = data;
    //表名下划线转小驼峰
    const smallName = name.replace(/_(.)/g, (a: string, b: string) => `${b.toLocaleUpperCase()}`);

    const str = `
import Router from '@koa/router';    
import ${name}Controller from '@/controllers/${smallName}.controller'
const ${smallName}Router = new Router({ prefix: '/${smallName}' });
${smallName}Router.post('/selectList', ${name}Controller.selectList);
${smallName}Router.post('/selectOne', ${name}Controller.selectOne);
${smallName}Router.post('/add', ${name}Controller.add);
${smallName}Router.post('/update', ${name}Controller.update);
${smallName}Router.post('/delete', ${name}Controller.delete);    
//${smallName}Router.post('/sort', ${name}Controller.sort);
export default ${smallName}Router;
        
        `
    await createFile(str, `routes/${smallName}.router.ts`)
    return str

}
export default createRouterCode